<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" border>
      <el-table-column prop="_id" label="ID" width="300"></el-table-column>
      <el-table-column prop="name" label="物品名称" align="center"></el-table-column>
      <el-table-column label="物品图片" align="center">
          <template slot-scope="scope">
              <img :src="scope.row.icon" width="75" />
          </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/items/edit/${scope.row._id}`)"
            type="text"
            size="small"
          >编辑</el-button>
          <el-button @click="removeHandle(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await this.$http.get("rest/items");
      this.tableData = res.data;
    },
    
    async removeHandle(row) {
      const confirmRes = await this.$confirm(
        `您确认要删除id[${row.name}]的分类?`,
        "提示",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      if (confirmRes === "confirm") {
        const res = await this.$http.delete(`rest/items/${row._id}`);
        this.$message({
          type: "success",
          message: "删除成功"
        });
        this.getList()
      } else {
        this.$message({
          type: "info",
          message: "取消了删除"
        });
      }
    }
  }
};
</script>

<style>
</style>